<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\facade\Db;
use think\facade\View;

class Goods
{
    public function goodAdd(){
        return view();
    }

    public function goodEdit(){
        $id=input('get.id');

        //单挑商品数据查询
        $good=Db::table('ecm_goods')->where('id','=',$id)->find();

        //分类下拉菜单查询
        $cateF=Db::table('ecm_category')->where('parentid','=','0')->select();
        $cateS=Db::table('ecm_category')->where('parentid','<>','0')->select();

        View::assign('good',$good);

        View::assign('cateF',$cateF);
        View::assign('cateS',$cateS);
        return view();
    }

    public function goodList(){

        //表单数据接收
        $key=input('post.keywords');
        $show=input('post.is_show');
        $hot=input('post.is_hot');
        $down=input('post.is_down');
        $cid=input('post.cid');
        $m1=input('post.money1')==""?0:input('post.money1');
        $m2=input('post.money2')==""?999999999:input('post.money2');

        //where条件拼接
        $where=[];
        if((isset($m1) && $m1<>'')||(isset($m2) && $m2<>'')){$where[]=['price_shop','between',$m1.','.$m2];}//金额范围查找
        if(input('?post.keywords')&&$key!=""){$where[]=['g.name','like','%'.$key.'%'];}//商品关键字模糊查询
        if(input('?post.is_show')&& $show!=""){$where[]=['is_show','=',$show];}//显示
        if(input('?post.is_hot')&&$hot!=""){$where[]=['is_hot','=',$hot];}//热门
        if(input('?post.is_down')&& count($down)==1){$where[]=['is_down','in',$down];}//上下架
        if(input('?post.cid')&&$cid!=""){$where[]=['cid','=',$cid];}//分类查询

        //商品查询
        $goods=Db::table('ecm_goods g')
            ->field('g.*,c.name as cname')
            ->join('ecm_category c','g.cid=c.id','left')
            ->where($where)->select();

        View::assign('goods',$goods);

        View::assign('down',$down);
        View::assign('show',$show);
        View::assign('hot',$hot);
        View::assign('keys',$key);
        View::assign('m1',$m1);
        View::assign('m2',$m2);

        //下拉菜单查询
        $cateF=Db::table('ecm_category')->where('parentid','=','0')->select();//一级分类下拉菜单查询
        $cateS=Db::table('ecm_category')->where('parentid','<>','0')->select();//二级下拉菜单查询

        View::assign('cateF',$cateF);
        View::assign('cateS',$cateS);
        return view();
    }
}
